package com.maomao.utils;

import java.util.Random;

/**
 * Created by admin on 2018/3/26.
 */
public class RandomUtil {
    private static final Random random = new Random();
    public static int getRandom(){
        return (int) (Math.random()*10);
    }
    public static void main(String[] args){
//        Random a=new Random();
//        for (int i=0;i<5;i++){
//            int b=a.nextInt(12);
//            System.out.println(b);
//        }
        for (int i=0;i<10000;i++) {
            System.out.println(getRandomPass(60));
        }
    }


    /**
     * 求成功概率
     * @param rate
     * @return
     */
    private static boolean getRandomPass(int rate) {
        if (rate > 100) {
            return true;
        }
        int num = RandomUtil(100,0);
        if (num < rate) {
            return true;
        }
        return false;
    }

    public static int RandomUtil(int max,int min) {

        int s = random.nextInt(max)%(max-min+1) + min;
        return s;
    }

    public static int getRandomKey(int[] rNum) {
        int all = 0;
        int[] var2 = rNum;
        int key = rNum.length;

        int tmp;
        int i;
        for(tmp = 0; tmp < key; ++tmp) {
            i = var2[tmp];
            all += i;
        }

        if (all == 0) {
            return random.nextInt(rNum.length);
        } else {
            int rnd = random.nextInt(all);
            key = 0;
            tmp = 0;

            for(i = 0; i < rNum.length; ++i) {
                tmp += rNum[i];
                if (rnd < tmp) {
                    key = i;
                    break;
                }
            }

            return key;
        }
    }


}
